package com.kimu.product_comment_service_consumer.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kimu.product_comment_service_interface.entity.ProductComment;
import com.kimu.product_comment_service_interface.service.ProductCommentService;
import com.kimu.user.entity.User;
import com.kimu.user.service.JwtService;
import com.kimu.user.service.UserService;

import jakarta.servlet.http.HttpServletRequest;

@RestController
public class ProductCommentController {
    
    @DubboReference
    private UserService userService;

    @DubboReference
    private ProductCommentService productCommentService;

    private JwtService jwtService;

    public ProductCommentController(JwtService jwtService) {
        this.jwtService = jwtService;
    }

    /*
     * 新增一个商品评论的接口，需要登录，需要商品id
     */
    @PostMapping(path = "/api/product/{productId}/comments")
    public ResponseEntity<ProductComment> createProductComment(HttpServletRequest request, @PathVariable("productId") Long productId, ProductComment productComment) {
        // 首先判断用户是否登录
        String username = jwtService.getUsername(request);
        if (null == username) {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
        User user = userService.findUserByUsername(username);
        if (null == user) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        // 设置商品评论的用户id为user_id
        productComment.setUserId(user.getId());
        // 设置商品评论对应的商品id
        productComment.setProductId(productId);
        int row = productCommentService.createProductComment(productComment);
        if (row == 0) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(productComment, HttpStatus.CREATED);
        }
    }

    /*
     * 获取一个商品评论的接口
     */
    @GetMapping(path = "/api/products/{productId}/comments/{id}")
    public ResponseEntity<ProductComment> getProductCommentById(@PathVariable("id") Long id) {
        ProductComment productComment = productCommentService.getProductCommentById(id);
        if (null == productComment) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<>(productComment, HttpStatus.OK);
        }
    }

    /*
     * 修改一个商品评论的接口，需要登录，并且商品评论必须是请求人的
     */
    @PutMapping(path = "/api/products/{productId}/comments/{id}")
    public ResponseEntity<ProductComment> updateProduct(HttpServletRequest request, @RequestBody ProductComment productComment) {
        String username = jwtService.getUsername(request);
        if (null == username) {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
        User user = userService.findUserByUsername(username);
        if (null == user) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        ProductComment oldProductComment = productCommentService.getProductCommentById(productComment.getId());
        if (oldProductComment.getUserId() != user.getId()) {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
        // 属于该用户，再进行修改
        int row = productCommentService.updateProductComment(productComment);
        if (row == 0) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(productComment, HttpStatus.OK);
        }
    }

    /*
     * 删除一个商品评论的接口，需要登录，并且商品必须是请求人的
     */
    @DeleteMapping(path = "/api/products/{productId}/comments/{id}")
    public ResponseEntity<ProductComment> deleteProductCommentById(HttpServletRequest request, @PathVariable("id") Long id) {
        String username = jwtService.getUsername(request);
        if (null == username) {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
        User user = userService.findUserByUsername(username);
        if (null == user) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        ProductComment oldProductComment = productCommentService.getProductCommentById(id);
        if (oldProductComment.getUserId() != user.getId()) {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
        // 使用逻辑删除，即将is_deleted设置为false
        int row = productCommentService.deleteProductCommentById(id);
        if (row == 0) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(oldProductComment, HttpStatus.OK);
        }
    }

    /*
     * 获取商品评论列表，使用分页查询，有页数参数可以选择
     */
    @GetMapping(path = "/api/products/{productId}/comments")
    public ResponseEntity<List<ProductComment>> getProductComments(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageNum") Integer pageNum) {
        Page<ProductComment> page = new Page<>(pageNo, pageNum);
        IPage<ProductComment> result = productCommentService.getProductComments(page);
        List<ProductComment> productComments = result.getRecords();
        return new ResponseEntity<>(productComments, HttpStatus.OK);
    }
}
